package com.caeit.service.bigscreen.impl

import com.caeit.dao.bigscreen.BigscreenConfigDao
import com.caeit.exception.BadRequestException
import com.caeit.model.bigscreen.BigscreenConfig
import com.caeit.service.bigscreen.BigscreenConfigService
import com.caeit.service.bigscreen.dto.BigscreenConfigQuery
import com.caeit.utils.PageUtil
import com.caeit.utils.QueryHelp
import com.caeit.utils.SecurityUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.Modifying
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Propagation
import org.springframework.transaction.annotation.Transactional
import java.util.*

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = [Exception::class])
open class BigscreenConfigServiceImpl : BigscreenConfigService {

    @Autowired
    lateinit var bigscreenConfigDao: BigscreenConfigDao
    @Autowired
    lateinit var jdbctpl :JdbcTemplate
    override fun paginQuery(query: BigscreenConfigQuery, pageable: Pageable): Map<String, Any?> {
        val page: Page<BigscreenConfig> = bigscreenConfigDao.findAll({ root, criteriaQuery, criteriaBuilder ->
            QueryHelp.getPredicate(
                root,
                query,
                criteriaBuilder
            )
        }, pageable)
        return PageUtil.toPage(page) as Map<String, Object>
    }

    override fun queryById(id: Long): BigscreenConfig {
        return bigscreenConfigDao.getOne(id)
    }

    @Transactional(rollbackFor = [Exception::class])
    override fun save(resource: BigscreenConfig): Boolean {
        if (resource.status === 1) {
            bigscreenConfigDao.findEnableData(1)?.let {
                throw BadRequestException("已存在启动的大屏！");
            }
        }

        resource.createdBy = SecurityUtils.getUserDetails()
        var baseUserInfo = SecurityUtils.getBaseUserInfo()
        baseUserInfo?.let {
            resource.createArea = it.areaCode
            resource.createDept = it.organCode
        }

        bigscreenConfigDao.saveAndFlush(resource)

        return true
    }
    @Transactional
    @Modifying
    override fun edit(resource: BigscreenConfig): Boolean {
        val oldres=bigscreenConfigDao.findById(resource.id).get()
        if (resource.status === 1) {
            bigscreenConfigDao.findEnableData(1)?.let {
                it.status=0
                bigscreenConfigDao.save(it)
            }
            oldres.lastModifiedDate= Date()
            oldres.status=resource.status
            oldres.setLastModifiedBy(SecurityUtils.getUserDetails())
            bigscreenConfigDao.saveAndFlush(oldres)
        }else{
            bigscreenConfigDao.saveAndFlush(resource)
        }


        return true
    }

    @Transactional(rollbackFor = [Exception::class])
    override fun deleteById(id: Long): Boolean {
        bigscreenConfigDao.deleteById(id)

        return true
    }

    override fun findAll(): List<BigscreenConfig> {
        return bigscreenConfigDao.findAll()
    }

    override fun queryEnableData(): BigscreenConfig {
        val ret = bigscreenConfigDao.findEnableData(1) ?: throw BadRequestException("没有启用的项目")
        return ret
    }
}